﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;

public partial class CommonPages_SelectOperDistRoleDeptUnite : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!X.IsAjaxRequest)
        {
            ConverData();
            BindGrid();
            BuildTreeRoot();
            BuildTreeRootRole();
            BuildTreeRootDept();
        }
    }

    #region 类型为人员时代码
    //构建部门树根节点
    private void BuildTreeRoot()
    {
        DataTable tmpDt = CommonLib.BLL.SYS_DEPT.GetListSub(" dept_id='00' and  del_flag =0 ", "dept_id").Tables[0];
        if (tmpDt.Rows.Count == 1)
        {
            Ext.Net.AsyncTreeNode root = new Ext.Net.AsyncTreeNode();
            root.Text = tmpDt.Rows[0]["dept_name"].ToString();
            root.NodeID = "A" + tmpDt.Rows[0]["dept_id"].ToString();
            root.Expanded = true;
            root.IconCls = "no-Icon";
            root.Checked = ThreeStateBool.False;
            if (tmpDt.Rows[0]["leaf"].ToString() == "0")
                root.Leaf = true;
            root.Draggable = true;
            this.TreePanel1.Root.Add(root);
        }
    }
    [DirectMethod]
    public string NodeLoad(string nodeID, bool ischeck)
    {
        nodeID = nodeID.Substring(1, nodeID.Length - 1);
        Ext.Net.TreeNodeCollection nodes = new Ext.Net.TreeNodeCollection();
        if (!string.IsNullOrEmpty(nodeID))
        {
            DataTable TreeData = CommonLib.BLL.SYS_DEPT.GetListSub(" del_flag =0 and rtrim(dept_id) like '" + nodeID.Trim() + "__' ", "dept_id").Tables[0];
            for (int i = 0; i < TreeData.Rows.Count; i++)
            {
                AsyncTreeNode asyncNode = new AsyncTreeNode();
                asyncNode.Text = TreeData.Rows[i]["dept_name"].ToString();
                asyncNode.NodeID = "A" + TreeData.Rows[i]["dept_id"].ToString();
                asyncNode.IconCls = "no-Icon";
                DataTable dtt = CommonLib.BLL.SYS_USER.GetList(" dept_id='" + TreeData.Rows[i]["dept_id"].ToString() + "'").Tables[0];
                if (TreeData.Rows[i]["leaf"].ToString() == "0" && dtt.Rows.Count == 0)
                    asyncNode.Leaf = true;
                else
                {
                    if (ischeck)
                        asyncNode.Checked = ThreeStateBool.True;
                    else
                        asyncNode.Checked = ThreeStateBool.False;
                }
                nodes.Add(asyncNode);
            }
            DataTable TmpTable = CommonLib.BLL.SYS_USER.GetList(" dept_id='" + nodeID.Trim() + "'").Tables[0];
            for (int j = 0; j < TmpTable.Rows.Count; j++)
            {
                Ext.Net.TreeNode treeNode = new Ext.Net.TreeNode();
                treeNode.Text = TmpTable.Rows[j]["user_name"].ToString();
                treeNode.NodeID = TmpTable.Rows[j]["user_id"].ToString();
                treeNode.Leaf = true;
                treeNode.IconCls = "no-Icon";
                if (ischeck)
                    treeNode.Checked = ThreeStateBool.True;
                else
                    treeNode.Checked = ThreeStateBool.False;
                nodes.Add(treeNode);
            }
        }
        return nodes.ToJson();
    }

    //递归得到部门下人员的函数
    private void GetUserDataTbByDeptId(string deptId)
    {
        //得到选中部门下的人员
        DataTable dttUser = CommonLib.BLL.SYS_USER.GetList(" dept_id='" + deptId + "'").Tables[0];
        if (dttUser.Rows.Count > 0)
            GetUserDataTb(dttUser);
        //得到选中部门下的部门下的人员
        DataTable dttDept = CommonLib.BLL.SYS_DEPT.GetListSub(" del_flag =0 and rtrim(dept_id) like '" + deptId + "__' ", "dept_id").Tables[0];
        if (dttDept.Rows.Count > 0)
        {
            for (int j = 0; j < dttDept.Rows.Count; j++)
            {
                GetUserDataTbByDeptId(dttDept.Rows[j]["dept_id"].ToString().Trim());
            }
        }
    }
    //得到选中部门下的人员的函数


    private void GetUserDataTb(DataTable dtmp)
    {
        DataTable tmptable = Session["dtUniteUser"] as DataTable;
        for (int i = 0; i < dtmp.Rows.Count; i++)
        {
            DataRow[] drArr = tmptable.Select(" NUMBER='" + dtmp.Rows[i]["user_id"].ToString() + "'");
            if (drArr.Length < 1)
            {
                DataRow dr = tmptable.NewRow();
                dr["TYPE"] = "0";
                dr["NUMBER"] = dtmp.Rows[i]["user_id"].ToString();
                dr["NAME"] = dtmp.Rows[i]["user_name"].ToString();
                tmptable.Rows.Add(dr);
            }
        }
        Session["dtUniteUser"] = tmptable;
    }

    #endregion

    #region 类型为角色时代码
    private void BuildTreeRootRole()
    {
        DataTable tmpDt = CommonLib.BLL.SYS_DEPT.GetListSub(" dept_id='00' and  del_flag =0 ", "dept_id").Tables[0];
        if (tmpDt.Rows.Count == 1)
        {
            Ext.Net.AsyncTreeNode root = new Ext.Net.AsyncTreeNode();
            root.Text = tmpDt.Rows[0]["dept_name"].ToString();
            root.NodeID = tmpDt.Rows[0]["dept_id"].ToString();
            root.Expanded = true;
            root.IconCls = "no-Icon";
            if (tmpDt.Rows[0]["leaf"].ToString() == "0")
                root.Leaf = true;
            root.Draggable = true;
            this.TreePanel2.Root.Add(root);
        }
    }

    [DirectMethod]
    public string NodeLoadWin(string nodeID)
    {
        Ext.Net.TreeNodeCollection nodes = new Ext.Net.TreeNodeCollection();
        if (!string.IsNullOrEmpty(nodeID))
        {
            DataTable TreeData = CommonLib.BLL.SYS_DEPT.GetListSub(" del_flag =0 and rtrim(dept_id) like '" + nodeID.Trim() + "__' ", "dept_id").Tables[0];
            for (int i = 0; i < TreeData.Rows.Count; i++)
            {
                AsyncTreeNode asyncNode = new AsyncTreeNode();
                asyncNode.Text = TreeData.Rows[i]["dept_name"].ToString();
                asyncNode.NodeID = TreeData.Rows[i]["dept_id"].ToString();
                asyncNode.IconCls = "no-Icon";
                if (TreeData.Rows[i]["leaf"].ToString() == "0")
                    asyncNode.Leaf = true;
                nodes.Add(asyncNode);
            }
            DataTable TmpTable = CommonLib.BLL.SYS_DEPT_ROLES.GetList(" rtrim(dept_id)='" + nodeID.Trim() + "'").Tables[0];
            for (int j = 0; j < TmpTable.Rows.Count; j++)
            {
                Ext.Net.TreeNode treeNode = new Ext.Net.TreeNode();
                treeNode.Text = TmpTable.Rows[j]["ROLE_NAME"].ToString();
                treeNode.NodeID = TmpTable.Rows[j]["ROLE_ID"].ToString();
                treeNode.Leaf = true;
                treeNode.IconCls = "no-Icon";
                treeNode.Checked = ThreeStateBool.False;
                nodes.Add(treeNode);
            }
        }
        return nodes.ToJson();
    }
    #endregion

    #region 类型为部门时代码
    private void BuildTreeRootDept()
    {
        DataTable tmpDt = CommonLib.BLL.SYS_DEPT.GetListSub(" dept_id='00' and  del_flag =0 ", "dept_id").Tables[0];
        if (tmpDt.Rows.Count == 1)
        {
            Ext.Net.AsyncTreeNode root = new Ext.Net.AsyncTreeNode();
            root.Text = tmpDt.Rows[0]["dept_name"].ToString();
            root.NodeID = tmpDt.Rows[0]["dept_id"].ToString();
            root.Expanded = true;
            root.IconCls = "no-Icon";
            if (tmpDt.Rows[0]["leaf"].ToString() == "0")
                root.Leaf = true;
            root.Draggable = true;
            root.Checked = ThreeStateBool.False;
            this.TreePanel3.Root.Add(root);
        }
    }
    [DirectMethod]
    public string DeptNodeLoad(string nodeID)
    {
        Ext.Net.TreeNodeCollection nodes = new Ext.Net.TreeNodeCollection();
        if (!string.IsNullOrEmpty(nodeID))
        {
            DataTable TreeData = CommonLib.BLL.SYS_DEPT.GetListSub(" del_flag =0 and rtrim(dept_id) like '" + nodeID.Trim() + "__' ", "dept_id").Tables[0];
            for (int i = 0; i < TreeData.Rows.Count; i++)
            {
                AsyncTreeNode asyncNode = new AsyncTreeNode();
                asyncNode.Text = TreeData.Rows[i]["dept_name"].ToString();
                asyncNode.NodeID = TreeData.Rows[i]["dept_id"].ToString();
                asyncNode.IconCls = "no-Icon";
                if (TreeData.Rows[i]["leaf"].ToString() == "0")
                    asyncNode.Leaf = true;
                asyncNode.Checked = ThreeStateBool.False;
                nodes.Add(asyncNode);
            }
        }
        return nodes.ToJson();
    }
    #endregion

    private void ConverData()
    {
        if (Request.QueryString["type"] != null && Request.QueryString["type"].ToString() == "multi")
        {
            if (Request.QueryString["seltedValues"] != null && Request.QueryString["seltedValues"].ToString() != "")
            {
                string seltedValues = Request.QueryString["seltedValues"].ToString();
                string[] arrValues = seltedValues.Split(';');
                DataTable dtmp = CreatTmpTable();
                for (int i = 0; i < arrValues.Length; i++)
                {
                    string[] arrChilds = arrValues[i].Split(',');
                    DataRow dr = dtmp.NewRow();
                    dr["TYPE"] = arrChilds[0];
                    dr["NUMBER"] = arrChilds[1];
                    dr["NAME"] = arrChilds[2];
                    dtmp.Rows.Add(dr);
                }
                Session["dtUniteUser"] = dtmp;
            }
            else
                Session["dtUniteUser"] = CreatTmpTable();
        }
        else
        {
            if (Session["dtUniteUser"] == null)
                Session["dtUniteUser"] = CreatTmpTable();
        }
    }

    private DataTable CreatTmpTable()
    {
        DataTable tmptable = new DataTable();
        tmptable.TableName = "tmpUserTable";
        tmptable.Columns.Add("TYPE", Type.GetType("System.String"));
        tmptable.Columns.Add("NUMBER", Type.GetType("System.String"));
        tmptable.Columns.Add("NAME", Type.GetType("System.String"));
        return tmptable;
    }

    //加载列表
    private void BindGrid()
    {
        DataTable dt = Session["dtUniteUser"] as DataTable;
        this.Store1.DataSource = dt;
        this.Store1.DataBind();
    }

    [DirectMethod]
    public void refrashDataEvent(string NodeIds, string type)
    {
        string[] menuno = NodeIds.Split(';');
        for (int i = 0; i < menuno.Length; i++)
        {
            switch (type)
            {
                case "0":
                    if (menuno[i].Substring(0, 1) == "A")
                    {
                        string deptId = menuno[i].Substring(1, menuno[i].Length - 1).Trim();
                        GetUserDataTbByDeptId(deptId);
                    }
                    else
                    {
                        DataTable dtUser = CommonLib.BLL.SYS_USER.GetList(" user_id='" + menuno[i] + "'").Tables[0];
                        if (dtUser.Rows.Count > 0)
                        {
                            DataTable tmptable = Session["dtUniteUser"] as DataTable;
                            DataRow[] drArr = tmptable.Select(" NUMBER='" + dtUser.Rows[0]["user_id"].ToString() + "'");
                            if (drArr.Length < 1)
                            {
                                DataRow dr = tmptable.NewRow();
                                dr["TYPE"] = "0";
                                dr["NUMBER"] = dtUser.Rows[0]["user_id"].ToString();
                                dr["NAME"] = dtUser.Rows[0]["user_name"].ToString();
                                tmptable.Rows.Add(dr);
                                Session["dtUniteUser"] = tmptable;
                            }
                        }
                    }
                    break;
                case "1":
                    DataTable dtRole = CommonLib.BLL.SYS_DEPT_ROLES.GetList(" ROLE_ID='" + menuno[i] + "'").Tables[0];
                    if (dtRole.Rows.Count > 0)
                    {
                        DataTable tmptable = Session["dtUniteUser"] as DataTable;
                        DataRow[] drArr = tmptable.Select(" NUMBER='" + dtRole.Rows[0]["ROLE_ID"].ToString() + "'");
                        if (drArr.Length < 1)
                        {
                            DataRow dr = tmptable.NewRow();
                            dr["TYPE"] = "1";
                            dr["NUMBER"] = dtRole.Rows[0]["ROLE_ID"].ToString();
                            dr["NAME"] = dtRole.Rows[0]["ROLE_NAME"].ToString();
                            tmptable.Rows.Add(dr);
                            Session["dtUniteUser"] = tmptable;
                        }
                    }
                    break;
                case "2":
                    DataTable dtDept = CommonLib.BLL.SYS_DEPT.GetList("del_flag =0 and  dept_id='" + menuno[i] + "'").Tables[0];
                    if (dtDept.Rows.Count > 0)
                    {
                        DataTable tmptable = Session["dtUniteUser"] as DataTable;
                        DataRow[] drArr = tmptable.Select(" NUMBER='" + dtDept.Rows[0]["DEPT_ID"].ToString() + "'");
                        if (drArr.Length < 1)
                        {
                            DataRow dr = tmptable.NewRow();
                            dr["TYPE"] = "2";
                            dr["NUMBER"] = dtDept.Rows[0]["DEPT_ID"].ToString();
                            dr["NAME"] = dtDept.Rows[0]["DEPT_NAME"].ToString();
                            tmptable.Rows.Add(dr);
                            Session["dtUniteUser"] = tmptable;
                        }
                    }
                    break;
            }
        }
        DataTable dtSource = Session["dtUniteUser"] as DataTable;
        Store1.DataSource = dtSource;
        Store1.DataBind();
    }

    protected void DelUser_Click(object sender, DirectEventArgs e)
    {
        int nCount = 0;
        RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
        nCount = sm.SelectedRows.Count;
        if (nCount == 0)
        {
            X.Msg.Alert("系统提示", "请先选择要删除的记录！").Show();
            return;
        }
        DataTable tmptable = Session["dtUniteUser"] as DataTable;
        foreach (SelectedRow row in sm.SelectedRows)
        {
            DataRow[] tR = tmptable.Select("NUMBER='" + row.RecordID + "'");
            tmptable.Rows.Remove(tR[0]);
        }
        tmptable.AcceptChanges();
        Session["dtUniteUser"] = tmptable;
        BindGrid();
    }


    //获得选取用户的名字和ID字符串


    [DirectMethod]
    public string GetReturnValues()
    {
        string result = "";
        string allresult = "";
        string flag = Request.QueryString["Flag"];
        DataTable dtGetUser = Session["dtUniteUser"] as DataTable;
        if (dtGetUser.Rows.Count > 0)
        {
            for (int i = 0; i < dtGetUser.Rows.Count; i++)
            {
                result += dtGetUser.Rows[i]["TYPE"].ToString() + ":" + dtGetUser.Rows[i]["NUMBER"].ToString() + ";";
            }
            result = result.TrimEnd(';') + "$";
            for (int i = 0; i < dtGetUser.Rows.Count; i++)
            {
                result += GetTypeName(dtGetUser.Rows[i]["TYPE"].ToString()) + ":" + dtGetUser.Rows[i]["NAME"].ToString() + ";";
            }
            result = result.TrimEnd(';');
            result = flag + "$" + result;
            for (int i = 0; i < dtGetUser.Rows.Count; i++)
            {
                allresult += dtGetUser.Rows[i]["TYPE"].ToString() + "," + dtGetUser.Rows[i]["NUMBER"].ToString() + "," + dtGetUser.Rows[i]["NAME"].ToString() + ";";
            }
            allresult = allresult.TrimEnd(';');
            result = result + "$" + allresult;
        }
        return result;
    }

    private string GetTypeName(string type)
    {
        string typeName = "";
        if (type == "0")
            typeName = "人员";
        if (type == "1")
            typeName = "角色";
        if (type == "2")
            typeName = "部门";
        return typeName;
    }
}
